package com.learn.springcloud.hystrixexplainindetailconsumer.request.command.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.springframework.web.client.RestTemplate;

/**
 * 通过继承的方式创建 HystrixCommand 的实现（在快速入门中使用的是@HystrixCommand创建HystrixCommand 的实现）
 * @Time : 2018/7/27 17:32
 * @Version : 1.0
 */
public class UserCommand extends HystrixCommand<String> {

    private RestTemplate restTemplate;

    private String name;

    public UserCommand(Setter setter, RestTemplate restTemplate, String name) {
        super(setter);
        this.restTemplate = restTemplate;
        this.name = name;
    }

    public UserCommand(HystrixCommandGroupKey group, int executionIsolationThreadTimeoutInMilliseconds, RestTemplate restTemplate, String name) {
        super(group, executionIsolationThreadTimeoutInMilliseconds);
        this.restTemplate = restTemplate;
        this.name = name;
    }

    @Override
    protected String run() throws Exception {
        System.out.println("UserCommand Running : " + this.name);
        String url = "http://hystrix-explain-in-detail-provider/request/command/say/hello?name={1}";
        String result = this.restTemplate.getForObject(url,String.class,this.name);
        System.out.println(result);
        return result;
    }

    @Override
    protected String getFallback(){
        return "error, " + this.name;
    }

}
